class RequestAndComplaint::ComplaintsController < ApplicationController
  # GET /request_and_complaint/complaints
  # GET /request_and_complaint/complaints.xml

  def index
    if (user_signed_in? && ([5,7,2].include?(current_user.role)))
      @title_view = 'Denuncias'
#      @request_and_complaint_complaints = RequestAndComplaint::Complaint.all
         if (user_signed_in? && ([7,2].include?(current_user.role)))
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state= 0 "])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state=  3 "])
      end

#      @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state= 0 "])

      @complaint_types = RequestAndComplaint::ComplaintType.all
      @security_people = Security::Person.all
      @security_user_roles = Security::UserRole.all
       @users = User.find(:all,:conditions=>["users.role = ?", "5"])
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @request_and_complaint_complaints }
        format.json  { render :json => @request_and_complaint_complaints }
      end
   else
     respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /request_and_complaint/complaints/1
  # GET /request_and_complaint/complaints/1.xml
  def show
    if (user_signed_in? && ([5,2,7].include?(current_user.role)))
      @title_view = 'Denuncias'
      @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @request_and_complaint_complaint }
        format.json  { render :json => @request_and_complaint_complaint }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /request_and_complaint/complaints/new
  # GET /request_and_complaint/complaints/new.xml
  def new

      @title_view = 'Denuncias'
      @request_and_complaint_complaint = RequestAndComplaint::Complaint.new
      @complaint_types = RequestAndComplaint::ComplaintType.all
      @departments = Shared::Department.all
      @provinces = []
      @districts = []
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @request_and_complaint_complaint }
      end

  end

  # GET /request_and_complaint/complaints/1/edit
  #  def edit
  #    @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
  #    @complaint_types = RequestAndComplaint::ComplaintType.all
  #    @departments = Shared::Department.all
  #    @provinces = []
  #    @districts = []
  #  end

  # POST /request_and_complaint/complaints
  # POST /request_and_complaint/complaints.xml
  def create
    @request_and_complaint_complaint = RequestAndComplaint::Complaint.new(params[:request_and_complaint_complaint])
    @complaint_types = RequestAndComplaint::ComplaintType.all
    @departments = Shared::Department.all

    if ! @request_and_complaint_complaint.department.blank?
      @provinces = Shared::Province.find(:all,:conditions=>["shared_provinces.deleted = ? and (shared_provinces.department = ? )",0,@request_and_complaint_complaint.department])
      if ! @request_and_complaint_complaint.province.blank?
        @districts = Shared::District.find(:all,:conditions=>["shared_districts.deleted = ? and (shared_districts.province = ? )",0,@request_and_complaint_complaint.province])
      else
        @districts = []
      end
    else
      @provinces = []
      @districts = []
    end

 #    @user = User.find(params[:id])
  
    respond_to do |format|
      if @request_and_complaint_complaint.save
        format.html { redirect_to(@request_and_complaint_complaint, :notice => 'Complaint was successfully created.') }
        format.xml  { render :xml => @request_and_complaint_complaint, :status => :created, :location => @request_and_complaint_complaint }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @request_and_complaint_complaint.errors, :status => :unprocessable_entity }
      end
    end

      ComplaintMailer.registered_email(@request_and_complaint_complaint).deliver
  end


  # PUT /request_and_complaint/complaints/1
  # PUT /request_and_complaint/complaints/1.xml
  #  def update
  #    @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
  #    @complaint_types = RequestAndComplaint::ComplaintType.all
  #    @departments = Shared::Department.all
  #    @provinces = []
  #    @districts = []
  #    respond_to do |format|
  #      if @request_and_complaint_complaint.update_attributes(params[:request_and_complaint_complaint])
  #        format.html { redirect_to(@request_and_complaint_complaint, :notice => 'Complaint was successfully updated.') }
  #        format.xml  { head :ok }
  #      else
  #        format.html { render :action => "edit" }
  #        format.xml  { render :xml => @request_and_complaint_complaint.errors, :status => :unprocessable_entity }
  #      end
  #    end
  #  end

  def change_provinces
    @request_and_complaint_complaint = RequestAndComplaint::Complaint.new    
    @provinces = Shared::Province.find(:all,:conditions=>{:deleted=>[0],:department=>params[:id]})
    @districts = []
    render :partial=>"provinces", :layout=>false
  end

  def change_districts
    @request_and_complaint_complaint = RequestAndComplaint::Complaint.new
    @districts = Shared::District.find(:all,:conditions=>{:deleted=>[0],:province=>params[:id]})
    render :partial=>"districts", :layout=>false
  end

  # DELETE /request_and_complaint/complaints/1
  # DELETE /request_and_complaint/complaints/1.xml
  #  def destroy
  #    @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
  #    @request_and_complaint_complaint.destroy
  #
  #    respond_to do |format|
  #      format.html { redirect_to(request_and_complaint_complaints_url) }
  #      format.xml  { head :ok }
  #    end
  #  end

  def aprove
    @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
    @request_and_complaint_complaint.state=1
    if (user_signed_in?)
       @request_and_complaint_complaint.user=current_user.id
    end
     
    @request_and_complaint_complaint.save

    #RegistrationRequestMailer.approved_email.deliver

    respond_to do |format|

       
      format.html { redirect_to(request_and_complaint_complaints_url) }
      format.xml  { head :ok  }
    end
  end

  def reject
    @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
    @request_and_complaint_complaint.state=2
    if (user_signed_in?)
       @request_and_complaint_complaint.user=current_user.id
    end
    @request_and_complaint_complaint.save
    #RegistrationRequestMailer.rejected_email.deliver
    respond_to do |format|
      format.html { redirect_to(request_and_complaint_complaints_url) }
      format.xml  { head :ok }
    end
  end
  

  def assign
    @request_and_complaint_complaint = RequestAndComplaint::Complaint.find(params[:id])
    @request_and_complaint_complaint.state=3
    responsable = params[:complaint_responsible]
   @request_and_complaint_complaint.responsable=responsable
    @request_and_complaint_complaint.save
    #RegistrationRequestMailer.rejected_email.deliver
    respond_to do |format|
      format.html { redirect_to(request_and_complaint_complaints_url) }
      format.xml  { head :ok }
    end
  end

  def update_table
    if (user_signed_in? && ([7].include?(current_user.role)))
    @search_id = params[:search_id]
    @search_combo = params[:search_complaint_type]
    @search_field = params[:field_name].to_s

    @search_state = params[:search_complaint_state]
case @search_field

	when "Nombre" then  @variable="request_and_complaint_complaints.name LIKE ? and "
	when "Codigo" then  @variable="request_and_complaint_complaints.id LIKE ? and "
	
    when "DNI" then  @variable="request_and_complaint_complaints.dni LIKE ? and "
  when "Todos" then  @variable="request_and_complaint_complaints.dni LIKE ? or request_and_complaint_complaints.name LIKE ? or request_and_complaint_complaints.id LIKE ? and "



	end
    if !(@search_field=="Todos")
    if ! @search_id.blank?
      if ! @search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ?  ",'%'+@search_id+'%',@search_combo, @search_state])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.state= ?",'%'+@search_id+'%', @search_state])
      end
    else
      if !@search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ? ",@search_combo,@search_state])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state= ? ",@search_state])
      end
    end
    else
     if ! @search_id.blank?
      if ! @search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ?  ",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%',@search_combo, @search_state])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.state= ?",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%', @search_state])
      end
    else
      if !@search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ? ",@search_combo,@search_state])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state= ? ",@search_state])
      end
    end


      end

    render :partial => "table"



    else
      if (user_signed_in? && ([5,2].include?(current_user.role)))
    @search_id = params[:search_id]
    @search_combo = params[:search_complaint_type]
    @search_field = params[:field_name].to_s

    @search_state = params[:search_complaint_state]
case @search_field

	when "Nombre" then  @variable="request_and_complaint_complaints.name LIKE ? and "
	when "Codigo" then  @variable="request_and_complaint_complaints.id LIKE ? and "

    when "DNI" then  @variable="request_and_complaint_complaints.dni LIKE ? and "
      when "Todos" then  @variable="request_and_complaint_complaints.dni LIKE ? or request_and_complaint_complaints.name LIKE ? or request_and_complaint_complaints.id LIKE ? and "




	end
    @user=current_user.id

      if !(@search_field=="Todos")
    if ! @search_id.blank?
      if ! @search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ? ",'%'+@search_id+'%',@search_combo, @search_state,@user])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ?",'%'+@search_id+'%', @search_state,@user])
      end
    else
      if !@search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ?",@search_combo,@search_state,@user])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ?",@search_state,@user])
      end
    end
      else
       if ! @search_id.blank?
      if ! @search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ? ",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%',@search_combo, @search_state,@user])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>[""+@variable+ "request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ?",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%', @search_state,@user])
      end
    else
      if !@search_combo.blank?
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ?",@search_combo,@search_state,@user])
      else
        @request_and_complaint_complaints = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state= ? and request_and_complaint_complaints.responsable= ?",@search_state,@user])
      end
    end

        end



    render :partial => "table"
  end
  end
  end

#   if (user_signed_in? && ([2].include?(current_user.role)))
#
#
#
#  end
#
end
